package com.news.wemedia.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.news.model.common.dtos.PageResponseResult;
import com.news.model.common.dtos.ResponseResult;
import com.news.model.common.enums.AppHttpCodeEnum;
import com.news.model.wemedia.dtos.SensitiveDto;
import com.news.model.wemedia.pojos.WmSensitive;
import com.news.wemedia.mapper.SensitiveMapper;
import com.news.wemedia.service.SensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class SensitiveServiceImp extends ServiceImpl<SensitiveMapper, WmSensitive> implements SensitiveService {

    @Autowired
    private SensitiveMapper sensitiveMapper;

    /**
     * 查询铭感词列表
     * @param sensitiveDto
     * @return
     */
    @Override
    public ResponseResult getList(SensitiveDto sensitiveDto) {

        if (sensitiveDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"缺少参数");
        }

        IPage page = new Page(sensitiveDto.getPage(),sensitiveDto.getSize());

        page(page,Wrappers.<WmSensitive>lambdaQuery()
                .like(WmSensitive::getSensitives,sensitiveDto.getName())
                .orderByDesc(WmSensitive::getCreatedTime)
        );

        ResponseResult responseResult = new PageResponseResult(sensitiveDto.getPage()
                , sensitiveDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 新增敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveOne(WmSensitive wmSensitive) {
        if (wmSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        log.info("开始添加{}",wmSensitive);
        String sensitives = wmSensitive.getSensitives();
        WmSensitive wmSensitive1 = sensitiveMapper.selectOne(Wrappers.<WmSensitive>lambdaQuery()
                .eq(WmSensitive::getSensitives, sensitives));
        if (wmSensitive1!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,sensitives+"已存在");
        }
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        sensitiveMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        if (wmSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
